import { IValidation } from "./IValidation";
import { MinLengthValidator } from "../Validator/MinLengthValidator";
import { RegularExpreesionValidator } from "../Validator/RegularExpreesionValidator";
import { IPersonState } from "../State";

export class AddressValidation implements IValidation {
  private readonly minLengthValidator: MinLengthValidator = new MinLengthValidator(5);
  private readonly zipCodeValidator: RegularExpreesionValidator = new RegularExpreesionValidator('^\\d{5}(?:-\\d{4})?$');

  public Validate(state: IPersonState, errors: string[]): void {
    if (!this.minLengthValidator.IsValid(state.Address1)) {
      errors.push('地址1必须超过5个字符');
    }
    if (!this.minLengthValidator.IsValid(state.Town)) {
      errors.push('城市必须超过5个字符');
    }
    if (!this.minLengthValidator.IsValid(state.Country)) {
      errors.push('区县必须超过5个字符');
    }
    if (!this.zipCodeValidator.IsValid(state.Postcode)) {
      errors.push('邮编是错误的');
    }
  }
}